const boom = require('boom')
const {
  validationResult
} = require('express-validator')

// can be reused by many routes
const validate = validations => {
  return async (req, res, next) => {
    await Promise.all(validations.map(validation => validation.run(req)))
    const err = validationResult(req)
    if (err.isEmpty()) {
      return next()
    }
    const [{
      msg
    }] = err.errors
    console.log(msg)
    return next(boom.badRequest(msg))
  }
}

module.exports = {
  validate
}
